package com.hs.controller;


import com.hs.config.R;
import com.hs.entity.Lesson;
import com.hs.entity.User;
import com.hs.service.LessonService;
import com.hs.tool.StringTools;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("lesson")
public class LessonController {
    @Autowired
    LessonService lessonService;

    @RequestMapping("insert")
    @ResponseBody
    public R insert(Lesson record, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (record == null) {
            return R.FAILURE("请填写用户名密码等关键信息", null);
        }
        if (StringTools.isEmpty(record.getName())) {
            model.addAttribute("msg", "请填写用户名");
            return R.FAILURE("请填写用户名", null);
        }
        if (record.getModelId() == null) {
            model.addAttribute("msg", "缺少关键条件model");
            return R.FAILURE("请填写model", null);
        }
        record.setUserId(user.getId());
        int i = lessonService.insertSelective(record);
        if (i > 0) {
            return R.SUCCESS("", i);
        }
        return R.FAILURE("", 0);
        }
    @RequestMapping("lessonList")
    @ResponseBody
    public R lessonList(Lesson record, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        record.setUserId(user.getId());

        record.setNoInclude("我是不null");
        record.setClassId(record.getClassId());

        List<Lesson> lessons = lessonService.selectByCondition(record);
        if(lessons ==null || lessons.size() == 0){
            return R.FAILURE("没有可关联课程", null);
        }
        return R.SUCCESS("", lessons);
        }






    }